package com.liuhaoqi.raletiveblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Comment;
import com.liuhaoqi.raletiveblog.entity.dto.CommentDto;
import com.liuhaoqi.raletiveblog.result.BlogPageResult;
import com.liuhaoqi.raletiveblog.result.PageResult;
import com.liuhaoqi.raletiveblog.result.WebRestResult;
import com.liuhaoqi.raletiveblog.result.business.PageResult.CommentMangPageResult;
import com.liuhaoqi.raletiveblog.service.BlogService;
import com.liuhaoqi.raletiveblog.service.CommentService;
import com.liuhaoqi.raletiveblog.utils.GenCommentNickName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Author liuhaoqi
 * @Date 2020/4/24 16:34
 * @Version 1.0
 */
@RestController
public class CommentsController {

    @Autowired
    CommentService commentService;
    @Autowired
    BlogService blogService;


    private String avatar="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2223585207,173657926&fm=26&gp=0.jpg";
    private String replyavatar="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1857639112,3999183658&fm=26&gp=0.jpg";
    @PostMapping("/getAllComment")
    public PageResult<CommentDto> getAllComment(@RequestParam("blogId") Long blogId,
                                              @RequestParam(value = "pagenum",defaultValue = "1") String pagenum,
                                              @RequestParam(value = "pagesize",defaultValue = "10") String pagesize) {

        PageHelper.startPage(Integer.parseInt(pagenum),Integer.parseInt(pagesize));
        List<CommentDto> commentsList = commentService.getCommentsList(blogId);
        PageInfo<CommentDto> pageInfo = new PageInfo<>(commentsList);
        PageResult<CommentDto> result = new PageResult<>();
        result.setData(pageInfo.getList());
        return result;
    }


    /**
     * 保存顶级评论 无父评论id
     * @param commentcontent
     * @param blogId
     * @param location
     * @return
     */
    @PostMapping("/publishComment")
    public  PageResult<CommentDto> publishComment(@RequestParam("commentContent") String commentcontent,
                                                  @RequestParam("blogId") Long blogId,
                                                  @RequestParam(value = "location" ,defaultValue = "银河系") String location) {
        //评论一次 就更新blog表中的评论数量
        blogService.updateBlogCommentNumByBLogId(blogId);
        //设置当前时间
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        //设置头像等信息
        Comment comment = new Comment();
        comment.setAvatar(avatar);
        comment.setConmentcontent(commentcontent);
        comment.setBlogId(blogId);
        comment.setLocation(location);
        comment.setCreateTime(new Date());
        System.out.println(new Date());
        comment.setParentCommentId(-1L);
        comment.setNickname(GenCommentNickName.getChineseName());
        //保存评论
        int i = commentService.insertCommet(comment);
        System.out.println(i);
        //保存成功后查询出所有数据出来
        PageResult<CommentDto> allComment = getAllComment2(blogId,1,10);
        return allComment;
    }

    /**
     * 保存回复评论的信息
     * @param commentcontent
     * @param blogId
     * @param location
     * @return
     */
    @PostMapping("/publishReply")
    public PageResult<CommentDto> publishReply(@RequestParam("commentContent") String commentcontent,
                                               @RequestParam("blogId") Long blogId,
                                               @RequestParam("parentId") String parentId,
                                               @RequestParam("respondent") String respondent,
                                               @RequestParam(value = "location" ,defaultValue = "银河系") String location) {
        //评论一次 就更新blog表中的评论数量
        blogService.updateBlogCommentNumByBLogId(blogId);
        String commentparentId = parentId.substring(1);
        //设置当前时间
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        //设置头像等信息
        Comment comment = new Comment();
        comment.setAvatar(replyavatar);
        comment.setConmentcontent(commentcontent);
        comment.setBlogId(blogId);
        comment.setLocation(location);
        comment.setCreateTime(new Date());
        System.out.println(new Date());
        comment.setParentCommentId(Long.parseLong(commentparentId));
        //用户登录后可以得到该用户的身份
        comment.setNickname(GenCommentNickName.getChineseName());
        comment.setRespondent(respondent);
        //保存评论
        int i = commentService.insertCommet(comment);
        System.out.println(i);
        //保存成功后查询出所有数据出来
        PageResult<CommentDto> allComment = getAllComment2(blogId,1,10);
        return allComment;
    }

    private PageResult<CommentDto> getAllComment2(Long blogId, int pagenum, int pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<CommentDto> commentsList = commentService.getCommentsList(blogId);
        PageInfo<CommentDto> pageInfo = new PageInfo<>(commentsList);
        PageResult<CommentDto> result = new PageResult<>();
        result.setData(pageInfo.getList());
        return result;
    }
    
    
    //实现留言墙的功能
    @GetMapping("/getPageLeaveMessage")
    public PageResult<CommentDto> getPageLeaveMessage(@RequestParam("pageName") String pageName,
                                                      @RequestParam(value = "pagenum",defaultValue = "1") String pagenum,
                                                      @RequestParam(value = "pagesize",defaultValue = "10") String pagesize) {
        Long blogId = null;
        if ("about".equals(pageName)) {
            blogId=-100L;
        }
        PageHelper.startPage(Integer.parseInt(pagenum),Integer.parseInt(pagesize));
        List<CommentDto> commentsList = commentService.getCommentsList(blogId);
        PageInfo<CommentDto> pageInfo = new PageInfo<>(commentsList);
        PageResult<CommentDto> result = new PageResult<>();
        result.setData(pageInfo.getList());
        return result;
    }

    @PostMapping("/publishLeaveMessage")
    public  PageResult<CommentDto> publishLeaveMessage(@RequestParam("leaveMessageContent") String leaveMessageContent,
                                                  @RequestParam("pageName") String pageName,
                                                  @RequestParam(value = "location" ,defaultValue = "银河系") String location) {
        Long blogId=null;
        if ("about".equals(pageName)) {
            blogId=-100L;
        }
        //设置当前时间
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        //设置头像等信息
        Comment comment = new Comment();
        comment.setAvatar(avatar);
        comment.setConmentcontent(leaveMessageContent);
        comment.setBlogId(blogId);
        comment.setLocation(location);
        comment.setCreateTime(new Date());
        System.out.println(new Date());
        comment.setParentCommentId(-1L);
        comment.setNickname(GenCommentNickName.getChineseName());
        //保存评论
        int i = commentService.insertCommet(comment);
        System.out.println(i);
        //保存成功后查询出所有数据出来
        PageResult<CommentDto> allComment = getAllComment2(blogId,1,10);
        return allComment;
    }



    @PostMapping("/publishLeaveMessageReply")
    public PageResult<CommentDto> publishLeaveMessageReply(@RequestParam("leaveMessageContent") String leaveMessageContent,
                                               @RequestParam("pageName") String pageName,
                                               @RequestParam("parentId") String parentId,
                                               @RequestParam("respondent") String respondent,
                                               @RequestParam(value = "location" ,defaultValue = "银河系") String location) {
        Long blogId=null;
        if ("about".equals(pageName)) {
            blogId=-100L;
        }
        String commentparentId = parentId.substring(1);
        //设置当前时间
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        //设置头像等信息
        Comment comment = new Comment();
        comment.setAvatar(replyavatar);
        comment.setConmentcontent(leaveMessageContent);
        comment.setBlogId(blogId);
        comment.setLocation(location);
        comment.setCreateTime(new Date());
        System.out.println(new Date());
        comment.setParentCommentId(Long.parseLong(commentparentId));
        //用户登录后可以得到该用户的身份
        comment.setNickname(GenCommentNickName.getChineseName());
        comment.setRespondent(respondent);
        //保存评论
        int i = commentService.insertCommet(comment);
        System.out.println(i);
        //保存成功后查询出所有数据出来
        PageResult<CommentDto> allComment = getAllComment2(blogId,1,10);
        return allComment;
    }

    @GetMapping("/admin/getCommentsMang")
    @ResponseBody
    public BlogPageResult<CommentMangPageResult> getCommentsMang(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                                                 @RequestParam(defaultValue = "10",value = "pageSize") int pageSize) {
        int start=(pageNum-1)*pageSize;
        int end=pageSize;
        BlogPageResult<CommentMangPageResult> result = new BlogPageResult<>();
//        List<CommentMangPageResult> commentsMang = commentService.getCommentsMang(start,pageSize);
//        result.setData(commentsMang);
//        result.setCode(0);
//        result.setCount(commentService.getCommentsMangList());
        List<CommentMangPageResult> commentsMangTest = commentService.getCommentsMangTest(start, end, pageSize);
        result.setCode(0);
        result.setCount(commentsMangTest.get(0).getCnt());
        result.setData(commentsMangTest);
        return result;
    }

    @PostMapping("/admin/deleteCommentByCommentId")
    @ResponseBody
    public WebRestResult deleteCommentByCommentId(@RequestParam("commentId") Long commentId) {
        int i = commentService.deleteCommentByCommentId(commentId);
        WebRestResult result = new WebRestResult();
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除id为" + commentId + "的评论成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除id为" + commentId + "的评论失败！");
        }
        return result;
    }

    @PostMapping("/admin/updateCommentByCommentId")
    @ResponseBody
    public WebRestResult updateCommentByCommentId(@RequestParam("commentId") Long commentId,
                                                  @RequestParam("parentCommentId") Long parentCommentId,
                                                  @RequestParam("commentAvatar") String commentAvatar,
                                                  @RequestParam("commentContent") String commentContent,
                                                  @RequestParam("blogId") Long blogId,
                                                  @RequestParam("location") String location,
                                                  @RequestParam("nickname") String nickname) {
        Comment comment = new Comment();
        comment.setCommentid(commentId);
        comment.setParentCommentId(parentCommentId);
        comment.setAvatar(commentAvatar);
        comment.setConmentcontent(commentContent);
        comment.setBlogId(blogId);
        comment.setLocation(location);
        comment.setNickname(nickname);
        WebRestResult result = new WebRestResult();
        int i = commentService.updateCommentByCommentId(comment);
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新评论成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新评论失败");
        }
        return result;
    }



}
